package com.vlong.town.controller.admin.tourist;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.tourist.TouristInfo;
import com.vlong.town.service.tourist.TouristInfoService;
import com.vlong.town.util.common.TownConstant;
import com.vlong.town.util.data.DataUtil;
import com.vlong.town.util.file.FileUpload;
import com.vlong.town.util.pagination.PageTag;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 旅游景点信息Controller
 * @author Assassin
 */
@Controller
@RequestMapping("/tourist")
public class TouristInfoController extends BaseController {

    // Fields

    @Autowired
    private TouristInfoService touristInfoService;// 旅游景点信息业务实现类

    /**
     * 进入旅游景点信息列表页面
     * @return tourist/list_tourist
     */
    @RequestMapping("/list")
    public String gotoTouristInfoPage(
            @RequestParam(required=false, value="pageNo") Integer pageNo,
            @RequestParam(required=false, value="pageSize") Integer pageSize,
            @RequestParam(required=false, value="title") String title,
            @RequestParam(required=false, value="content") String content,
            @RequestParam(required=false, value="desc") String desc) {
        PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
        PageHelper.orderBy("update_time DESC");
        List<TouristInfo> touristInfoList = this.touristInfoService.queryList(null,null,null,null,title,content,desc,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null);
        PageInfo<TouristInfo> pageInfo = new PageInfo<TouristInfo>(touristInfoList);
        PageTag<TouristInfo> pt = new PageTag<TouristInfo>(request, "list", pageInfo);
        request.setAttribute("pageHtml", pt.getPageHtml());
        request.setAttribute("title", title);
        request.setAttribute("content", content);
        request.setAttribute("desc", desc);
        for(TouristInfo touristInfo : touristInfoList) {
            touristInfo.setContent(DataUtil.delHTMLTag(touristInfo.getContent(), 0));
        }
        request.setAttribute("touristInfoList", touristInfoList);
        return "admin/tourist/list_tourist";
    }

    /**
     * 进入旅游景点信息添加页面
     * @return tourist/add_tourist
     */
    @GetMapping("/addTourist")
    public String gotoTouristInofAddPage() {
        return "admin/tourist/add_tourist";
    }

    /**
     * 进入旅游景点信息修改页面
     * @return tourist/edit_tourist
     */
    @GetMapping("/editTourist/{touristId}")
    public String gotoTouristInofEditPage(@PathVariable( value="touristId") Long touristId) {
        TouristInfo touristInfo = this.touristInfoService.queryById(touristId);
        request.setAttribute("touristInfo", touristInfo);
        List<String> thumbnailUrls = new ArrayList<>();
        String[] thumbnail;
        if(touristInfo.getThumbnail()!=null&&!touristInfo.getThumbnail().equals("")){
            thumbnail=touristInfo.getThumbnail().split(",");
            for(String p:thumbnail){
                thumbnailUrls.add(request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/" +"cdkeditor"+ "/" +p);
            }
            request.setAttribute("thumbnailSize",thumbnail.length);
        }
        request.setAttribute("thumbnailUrls",thumbnailUrls);
        return "admin/tourist/edit_tourist";
    }

    /**
     * 添加旅游景点信息
     * @param title 旅游景点标题
     * @param content 旅游景点内容
     * @param desc 旅游景点简介
     * @param picture 缩略图
     * @param feature 旅游景点特征
     * @param address 旅游景点位置
     * @param lng 旅游景点经度
     * @param lat 旅游景点纬度
     * @return DataMessage->Json
     */
   /* @ResponseBody
    @PostMapping("executeAddTourist")
    public DataMessage addTourist(
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="desc", required=true) String desc,
            @RequestParam(name="picture", required=true) String picture,
            @RequestParam(name="feature", required=true) String feature,
            @RequestParam(name="address", required=true) String address,
            @RequestParam(name="lng", required=true) String lng,
            @RequestParam(name="lat", required=true) String lat) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("旅游景点标题不合法");
            return dm;
        }
        this.touristInfoService.add(null,null,null,null,title,content,desc,picture,feature,address,Float.parseFloat(lng),Float.parseFloat(lat),System.currentTimeMillis(),null,null,null,null,null,null,null,0,null,null);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("旅游景点添加成功");
        return dm;
    }*/
    @ResponseBody
    @PostMapping("executeAddTourist")
    public DataMessage addTourist(TouristInfo touristInfo) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(touristInfo.getTitle(), 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("旅游景点标题不合法");
            return dm;
        }
        touristInfo.setTownId(getOrgId()==null? 1 :getOrgId());
        Long start = this.touristInfoService.addTourist(touristInfo);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("旅游景点添加成功");
        dm.setData(start);
        return dm;
    }

    /**
     * 修改旅游景点信息
     * @param title 旅游景点标题
     * @param content 旅游景点内容
     * @param desc 旅游景点简介
     * @param picture 缩略图
     * @param feature 旅游景点特征
     * @param address 旅游景点位置
     * @param lng 旅游景点经度
     * @param lat 旅游景点纬度
     * @return DataMessage->Json
     */
    /*@ResponseBody
    @PostMapping("executeEditTourist")
    public DataMessage editTourist(
            @RequestParam(name="touristId", required=true) Long touristId,
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="desc", required=true) String desc,
            @RequestParam(name="picture", required=true) String picture,
            @RequestParam(name="feature", required=true) String feature,
            @RequestParam(name="address", required=true) String address,
            @RequestParam(name="lng", required=true) String lng,
            @RequestParam(name="lat", required=true) String lat) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("旅游景点标题不合法");
            return dm;
        }
        this.touristInfoService.edit(touristId,null,null,null,title,content,desc,picture,feature,address,Float.parseFloat(lng),Float.parseFloat(lat),null,System.currentTimeMillis(),null,null,null,null,null,null,0,null,null);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("旅游景点修改成功");
        return dm;
    }*/

    @ResponseBody
    @PostMapping("executeEditTourist")
    public DataMessage editTourist(TouristInfo touristInfo) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(touristInfo.getTitle() , 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("旅游景点标题不合法");
            return dm;
        }
        this.touristInfoService.updateTourist(touristInfo);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("旅游景点修改成功");
        return dm;
    }

    /**
     * 删除旅游景点
     * @param touristInfo 旅游实体
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/delete")
    public DataMessage deleteUser(TouristInfo touristInfo) {
        DataMessage dm = new DataMessage();
        if(touristInfo.getTouristId() != null && touristInfo.getTouristId() > 0L) {
            touristInfo.setIsDel(1);
            this.touristInfoService.updateTourist(touristInfo);
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("旅游景点删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }
    /**
     * 禁用旅游景点
     * @param touristInfo 旅游实体
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/disable")
    public DataMessage disable(TouristInfo touristInfo) {
        DataMessage dm = new DataMessage();
        if(touristInfo.getTouristId() != null && touristInfo.getTouristId() > 0L) {
            touristInfo.setState(1);
            this.touristInfoService.updateTourist(touristInfo);
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("旅游景点禁用成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }
/**
     * 启用旅游景点
     * @param touristInfo 旅游实体
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/startUsing")
    public DataMessage startUsing(TouristInfo touristInfo) {
        DataMessage dm = new DataMessage();
        if(touristInfo.getTouristId() != null && touristInfo.getTouristId() > 0L) {
            touristInfo.setState(0);
            this.touristInfoService.updateTourist(touristInfo);
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("旅游景点启用成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }

    /**
     * 批量删除旅游景点
     * @param touristIds 主键值集合字符串
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/batchDelete")
    public DataMessage batchDeleteUser(@RequestParam( value="touristIds") String touristIds) {
        DataMessage dm = new DataMessage();
        if(this.validateStringProperty(touristIds, 1, 255)) {
            String[] touristIdArray = touristIds.split(",");
            for(String touristId : touristIdArray) {
                this.touristInfoService.delete(Long.parseLong(touristId));
            }
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("旅游景点删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }

    /**
     * 上传图片轮播图
     * @return DataMessage
     */
    @ResponseBody
    @RequestMapping(value = "/upload", method= RequestMethod.POST)
    public String upload(@RequestParam MultipartFile file) throws Exception{
        return FileUpload.upload(file);
    }

}
